﻿#region Copyright RenGuiYou. All rights reserved.
//=====================================================
// NeatlyFrameWork
// Author:      RenGuiyou
// Feedback: 	mailto:750539605@qq.com
//=====================================================
#endregion

using System;
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.EventSystems;

namespace Neatly.UI
{
    [ExecuteInEditMode]
    [DisallowMultipleComponent]
    public class NButton : NButtonBase, IPointerClickHandler, IPointerDownHandler, IPointerUpHandler
    {

        private Action m_OnClick; 
        private Action m_OnPointerDown;
        private Action m_OnPointerUp;

        private Vector2 m_pointPos = new(0, 0);
        public Vector2 pointPosition
        {
            get => m_pointPos;
            set => m_pointPos = value;
        }

        public void OnPointerDown(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;
            m_CurrentSelectionState = SelectionState.Pressed;
            if (m_ScaleTween)
            {
                UITween.Scale(gameObject, m_SrcScale * NeatlyUI.ButtonScaleSize, NeatlyUI.ButtonTweenTime);
            }
            OnSetProperty();
            if (m_OnPointerDown != null)
            {
                m_pointPos = eventData.position;
                m_OnPointerDown.Invoke();
            }
        }

        public void OnPointerUp(PointerEventData eventData)
        {
            if (!IsActive() || !IsInteractable())
                return;
            m_CurrentSelectionState = SelectionState.Normal;
            if (m_ScaleTween)
            {
                UITween.Scale(gameObject, m_SrcScale, NeatlyUI.ButtonTweenTime);
            }
            OnSetProperty();
            if (m_OnPointerUp!=null)
            {
                m_pointPos = eventData.position;
                m_OnPointerUp.Invoke();
            }
        }

        public void OnPointerClick(PointerEventData eventData)
        {
            m_pointPos = eventData.position;
            Press();
        }

        protected void Press()
        {
            if (!IsActive() || !IsInteractable())
                return;
            if (m_SoundEnable)
                NeatlyUI.PlaySoundAction(m_SoundName);
            if (m_OnClick != null)
            {
                m_OnClick.Invoke();
            }
        }

        public void AddClickListener(Action action)
        {
            m_OnClick = action;
        }

        public void AddPointerDownListener(Action action)
        {
            m_OnPointerDown = action;
        }

        public void AddPointerUpListener(Action action)
        {
            m_OnPointerUp = action;
        }


#if UNITY_EDITOR || UNITY_STANDALONE_WIN
        void Update()
        {
            if (Input.GetKeyUp(virtualKeyCode))
            {
                if (m_OnPointerUp != null)
                {
                    m_OnPointerUp.Invoke();
                }
            }
            if (Input.GetKeyDown(virtualKeyCode))
            {
                if (m_OnPointerDown != null)
                {
                    m_OnPointerDown.Invoke();
                }

                if (m_OnClick != null)
                {
                    m_OnClick.Invoke();
                }
            }
        }
#endif
    }
}

